<?php
namespace Core;

class Response {
    protected $statusCode = 200;
    protected $headers = [];
    protected $body;
    
    public function status($code) {
        $this->statusCode = $code;
        return $this;
    }
    
    public function header($name, $value) {
        $this->headers[$name] = $value;
        return $this;
    }
    
    public function json($data) {
        $this->header('Content-Type', 'application/json');
        $this->body = json_encode($data);
        return $this;
    }
    
    public function send() {
        http_response_code($this->statusCode);
        
        foreach ($this->headers as $name => $value) {
            header("$name: $value");
        }
        
        echo $this->body;
    }
    
    public static function make() {
        return new static();
    }
}